<?php
	class CommonGeneratorTest extends PHPUnit_Framework_TestCase
	{
		protected $_cmn_gen;
		protected $_config;

		protected function setUp() 
		{ 
			$this->_cmn_gen = new CommonGenerator();
			$this->_config = Zend_Registry::get('config');
		}
		
		protected function tearDown() 
		{ 
			unset($this->_cmn_gen);
			unset($this->_config);
		}

		public function testGenerateUniqueId()
		{
			$id = $this->_cmn_gen->generateUniqueId();
			$this->assertNotNull($id);
			
			$id_pieces = split('_', $id);
			$this->assertTrue(sizeof($id_pieces) == 3);	

			foreach($id_pieces as $id_piece)
				$this->assertNotNull($id_piece);

			$this->assertTrue(strlen($id_pieces[0]) == 10);
			$this->assertTrue(strlen($id_pieces[1]) == 1 ||
				strlen($id_pieces[1]) == 2);
		}
		
		public function testGenerateUniqueIdWithIndex()
		{
			$id = $this->_cmn_gen->generateUniqueId(1);
			$this->assertNotNull($id);
			
			$id_pieces = split('_', $id);
			$this->assertTrue(sizeof($id_pieces) == 4);	

			foreach($id_pieces as $id_piece)
				$this->assertNotNull($id_piece);

			$this->assertTrue(strlen($id_pieces[0]) == 10);
			$this->assertTrue(strlen($id_pieces[1]) == 1 ||
				strlen($id_pieces[1]) == 2);
			$this->assertTrue(strlen($id_pieces[3]) == 1);
			$this->assertTrue($id_pieces[3] == 1);
		}
		
		public function testGenerateCurrentIso8601Dates()
		{
			$dates = $this->_cmn_gen->generateCurrentIso8601Dates();
			$this->assertTrue(sizeof($dates) == 2);

			foreach ($dates as $date)
			{
				$count = preg_match('/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d-\d\d:\d\d$/', $date);
				$this->assertTrue($count == 1);
			}
		}
		
		public function testGenerateFilenameFromContentType()
		{
			$content_types = array(
				'jpeg' => 'image/jpeg', 
				'gif' => 'image/gif', 
				'png' => 'image/x-png', 
				'3g2' => 'video/3gpp2'
			);
			
			foreach($content_types as $extension => $content_type)
			{
				$filename = $this->_cmn_gen->generateFilenameFromContentType('file', $content_type);
				$this->assertNotNull($filename);
				
				$filename_pieces = split('\.', $filename);
				$this->assertTrue($filename_pieces[0] == 'file');
				$this->assertTrue($filename_pieces[1] == $extension);
			}
		}
		
		public function testGenerateFilenameFromContentTypeException()
		{
			$this->setExpectedException('CommonGeneratorException'); 
			$filename = $this->_cmn_gen->generateFilenameFromContentType('file', 'whoops');
		}
	}
